Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ci/pr-review): use workflow_call instead of workflow_run event #16891

Merged
merged 4 commits into from
Dec 8, 2023

Conversation

yin1999
Copy link
Member

@yin1999 yin1999 commented Nov 10, 2023

Description

This PR mainly switch to workflow_call instead of workflow_run event to prevent calling "PR review companion" when not necessary.

To achieve this, and take this opportunity to improve the code, the following changes were made:

  1. Use pull_request_target event instead of pull_request, with the former event, the GITHUB_TOKEN has write permissions and the jobs could access GitHub secrets (we are using secrets to store the AK, SK).
  2. Set the permissions of the "tests" job to read-all to avoid accidental writes to the repo.
  3. Run the "PR review companion" as a spread job (named "review") in "PR test", so we could use if condition to skip unnecessary runs,
    Set the permissions of "review" job to write-all, so the deployer could create comment in PRs.
    Add secrets: inherit to inherit the secrets from the parent workflow so the reusable workflow could access all secrets. (tested in: remove some lines for test yin1999/content#8, https://github.com/yin1999/content/actions/runs/6769815012/job/18397036827?pr=8#step:10:34)
  4. Use actions/download-artifact instead of js scripts, as the shared "PR review companion" workflow is a spread job in "PR test" now, we could use this action to download and unzip artifacts (simplifies the steps).
  5. Remove the path matches for pr-test workflow, as this workflow is a required ci test (we should not skip it).

Security statement

Because the GITHUB_TOKEN generated in workflows that triggered by pull_request_target event has write permissions, we need to avoid malicious damage to the repo by the PR author.

  1. Set permissions to different jobs: as the first job would run js scripts which could be modified by PR authors, set the permission of this job to read-all (as we still need the GITHUB_TOKEN to access some resouces).
  2. The pull_request_target event would trigger the workflow runs with the main version of workflows (including the shared workflows - "PR review companion"). This has been tested by Onkar - Test workflow_call yin1999/content#6, and has been documented by GitHub Docs:

    Although the workflow runs in the context of the base of the pull request, you should make sure that you do not check out, build, or run untrusted code from the pull request with this event.

  3. The workflow triggered by pull_request_target event could also access GitHub secrets. But GitHub Actions can only read a secret if you explicitly include the secret in a workflow.

Related issues and pull requests

Reflect: mdn/content#30064
Tested in my fork: yin1999#29

@github-actions github-actions bot added the system Infrastructure and configuration for the project label Nov 10, 2023
if: ${{ env.GIT_DIFF_CONTENT }}
run: |
# Save the raw diff blob and store that inside the ./build/
# Download the raw diff blob and store that inside the build
# directory.
# The purpose of this is for the PR Review Companion to later
# be able to use this raw diff file for the benefit of analyzing.
wget https://github.com/${{ github.repository }}/compare/${{ env.BASE_SHA }}...${{ env.HEAD_SHA }}.diff -O ${{ env.BUILD_OUT_ROOT }}/DIFF
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge the steps, align with the mdn/content's workflow

As the `pr-test` workflow is now a required ci test.
This should be triggered in any PRs.
@yin1999
Copy link
Member Author

yin1999 commented Dec 5, 2023

@bsmth would you mind to take a look, thanks 🤗

bsmth
bsmth previously approved these changes Dec 5, 2023
Copy link
Member

@bsmth bsmth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just diffed with https://www.diffchecker.com/EzXNI3G4/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight differences here: https://www.diffchecker.com/bU2WdBmN/, you may want to grab the latest version from main as there were some bugfixes on "non-markdown only" changed files, e.g.:

if: ${{ env.GIT_DIFF_CONTENT }} || ${{ env.GIT_DIFF_FILES }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which reminds me that these workflows should probably (eventually) live in https://github.com/mdn/workflows as they will diverge over time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in d9b1071

@bsmth bsmth self-requested a review December 5, 2023 09:48
@bsmth bsmth dismissed their stale review December 5, 2023 09:48

Some deviations from the content repo that would be nice to have covered before we merge

Copy link
Member

@bsmth bsmth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@yin1999 yin1999 merged commit 01127e2 into mdn:main Dec 8, 2023
6 of 7 checks passed
@yin1999 yin1999 deleted the pr-review branch December 8, 2023 14:35
@yin1999
Copy link
Member Author

yin1999 commented Dec 8, 2023

Check the workflow run here: https://github.com/mdn/translated-content/actions/runs/7142643657/job/19452360341?pr=17304

It works well. Thank you @bsmth ❤️

@bsmth
Copy link
Member

bsmth commented Dec 11, 2023

Well done 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
system Infrastructure and configuration for the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants